package com.seed.codegenerator.generator.system;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecgframework.codegenerate.database.DbReadTableUtil;
import org.jeecgframework.codegenerate.generate.impl.CodeGenerateOne;
import org.jeecgframework.codegenerate.generate.pojo.ColumnVo;
import org.jeecgframework.codegenerate.generate.pojo.TableVo;
import org.jeecgframework.codegenerate.generate.util.NonceUtils;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author gchiaway
 * 日期: 2021-01-07
 * 时间: 15:30
 */
@Slf4j
public class SystemCodeGenerateOne extends CodeGenerateOne {

    private final TableVo tableVo;
    private List<ColumnVo> columns;
    private List<ColumnVo> originalColumns;
    private Map<String, String> extendParams;

    public SystemCodeGenerateOne(TableVo tableVo) {
        super(tableVo);
        this.tableVo = tableVo;
    }

    public SystemCodeGenerateOne(TableVo tableVo, List<ColumnVo> columns, List<ColumnVo> originalColumns) {
        super(tableVo, columns, originalColumns);
        this.tableVo = tableVo;
        this.columns = columns;
        this.originalColumns = originalColumns;
    }

    public SystemCodeGenerateOne(TableVo tableVo, List<ColumnVo> columns, List<ColumnVo> originalColumns, Map<String, String> extendParams) {
        super(tableVo, columns, originalColumns);
        this.tableVo = tableVo;
        this.columns = columns;
        this.originalColumns = originalColumns;
        this.extendParams = extendParams;
    }


    @Override
    public Map<String, Object> a() throws Exception {
        HashMap var1 = new HashMap();
        var1.put("bussiPackage", org.jeecgframework.codegenerate.a.a.g);
        var1.put("entityPackage", this.tableVo.getEntityPackage());
        var1.put("entityName", this.tableVo.getEntityName());
        var1.put("tableName", this.tableVo.getTableName());
        var1.put("primaryKeyField", org.jeecgframework.codegenerate.a.a.l);
        if (this.tableVo.getFieldRequiredNum() == null) {
            this.tableVo.setFieldRequiredNum(StringUtils.isNotEmpty(org.jeecgframework.codegenerate.a.a.m) ? Integer.parseInt(org.jeecgframework.codegenerate.a.a.m) : -1);
        }

        if (this.tableVo.getSearchFieldNum() == null) {
            this.tableVo.setSearchFieldNum(StringUtils.isNotEmpty(org.jeecgframework.codegenerate.a.a.n) ? Integer.parseInt(org.jeecgframework.codegenerate.a.a.n) : -1);
        }

        if (this.tableVo.getFieldRowNum() == null) {
            this.tableVo.setFieldRowNum(Integer.parseInt(org.jeecgframework.codegenerate.a.a.p));
        }

        var1.put("tableVo", this.tableVo);

        try {
            if (this.columns == null || this.columns.size() == 0) {
                this.columns = DbReadTableUtil.a(this.tableVo.getTableName());
            }

            var1.put("columns", this.columns);
            if (this.originalColumns == null || this.originalColumns.size() == 0) {
                this.originalColumns = DbReadTableUtil.readOriginalTableColumn(this.tableVo.getTableName());
            }

            var1.put("originalColumns", this.originalColumns);
            Iterator var2 = this.originalColumns.iterator();

            while(var2.hasNext()) {
                ColumnVo var3 = (ColumnVo)var2.next();
                if (var3.getFieldName().toLowerCase().equals(org.jeecgframework.codegenerate.a.a.l.toLowerCase())) {
                    var1.put("primaryKeyPolicy", var3.getFieldType());
                }
            }
        } catch (Exception var4) {
            throw var4;
        }

        long var5 = NonceUtils.c() + NonceUtils.g();
        if (!CollectionUtils.isEmpty(extendParams)) {
            var1.putAll(extendParams);
        }
        var1.put("serialVersionUID", String.valueOf(var5));
        return var1;
    }

}
